package com.lanyuan.a.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.atest.UserFormMap;
import com.lanyuan.a.atest.UserGroupsFormMap;
import com.lanyuan.a.bean.Article;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.service.ArticleService;
import com.lanyuan.a.service.ButtonService;
import com.lanyuan.a.service.union.AdminService;
import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.exception.SystemException;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;

@Controller
@RequestMapping("/admin/")
public class AdminController extends BaseController {
	@Autowired
	ArticleService articleService;
	@Resource
	ButtonService buttonService;
	@Resource
	AdminService adminService;
	
	@RequestMapping("article")
	public String listUI(Model model) throws Exception {
		model.addAttribute("buttons", buttonService.getButtons("#article"));
		return Common.BACKGROUND_PATH + "/system/admin/list";
	}

	@ResponseBody
	@RequestMapping("findArticleByPage")
	public PageView findArticleByPage(String pageNow,String pageSize) throws Exception {
		System.out.println();
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		
		List<Article> list = articleService.queryByPage(baseFormMap);
		pageView.setRecords(adminService.updateTitle(list));//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/usermanage/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String addEntity(String txtGroupsSelect){
		try {
			System.out.println("addEntity");
			UserFormMap userFormMap = getFormMap(UserFormMap.class);
			String username = (String)userFormMap.get("userName");
			String accountname = (String)userFormMap.get("accountName");
			String password = (String)userFormMap.get("password");
			User user = new User();
			user.setUsername(username);
			user.setAccountname(accountname);
			user.setPassword(password);
		} catch (Exception e) {
			 throw new SystemException("添加账号异常");
		}
		return "success";
	}

	
	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("article", articleService.get(Integer.parseInt(id)));
		}
		return Common.BACKGROUND_PATH + "/system/admin/edit";
	}
	
	
	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String editEntity(String txtGroupsSelect) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
//		baseFormMap.update();
	
		String id = (String)baseFormMap.get("id");
		String title = (String)baseFormMap.get("title");
		String tag = (String)baseFormMap.get("tag");
		String content = (String)baseFormMap.get("content");
		adminService.saveArticle(id , title , tag ,content);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for(String a : ids){
			String[] arr = a.split(",");
			articleService.deleteByIds(arr);
		}
		return "success";
	}
}